package edu.nf.project.webs.storeOperation;

import edu.nf.project.entitys.entity.other.dto.CustomerOrderInfoDTO;
import edu.nf.project.services.exception.derivedAnomalies.CustomerOrderException;
import edu.nf.project.services.response.BaseController;
import edu.nf.project.services.response.ResultVO;
import edu.nf.project.services.storeOperation.CustomerOrderDTOService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author gjh
 * @Date 2025/6/4
 * @Description:
 */
@RestController
@RequestMapping("/api/ordersDTO")
public class CustomerOrderDTOController extends BaseController {
    @Autowired
    private CustomerOrderDTOService customerOrderDTOService;

    /*
        根据u_id查询订单详情 —— 用户端
    */
    @GetMapping("/orderDto")
    public ResultVO<List<CustomerOrderInfoDTO>> getOrderByUId(@RequestParam() String t_id) {
        try {
            List<CustomerOrderInfoDTO> list = customerOrderDTOService.getOrderByOrderUId(t_id);
            if (list == null) {
                throw new CustomerOrderException(30001, "订单不存在");
            }
            return success(list);
        } catch (Exception e) {
            if (e instanceof CustomerOrderException) {
                throw e;
            }
            System.out.println(e);
            throw new CustomerOrderException(30000, "系统错误，请稍后重试");
        }
    }

    @GetMapping("{orderId}")
    public ResultVO<CustomerOrderInfoDTO> getOrderById(@PathVariable String orderId) {
        try {
            CustomerOrderInfoDTO orderData = customerOrderDTOService.getOrderById(orderId);
            if (orderData == null) {
                throw new CustomerOrderException(30001, "订单不存在");
            }
            return success(orderData);
        } catch (Exception e) {
            if (e instanceof CustomerOrderException) {
                throw e;
            }
            throw new CustomerOrderException(30000, "系统错误，请稍后重试");
        }
    }
}